<template>
	<view>
		<!-- <view v-if="roleslist.length">
			<u-subsection active-color="#ff9900" :list="roleslist" :current="current" @change="change" v-if="roleId == '3'"></u-subsection>
		</view> -->
		
		<view>
			<u-subsection active-color="#ff9900" :list="roleslist" :current="current" @change="change" v-if="roleId == '3'"></u-subsection>
		</view>
		
		<view class="header">
			<view class="u-flex-nowrap u-row-between u-flex page-font">
				<view class="u-flex">请输入条码信息或扫码</view>
			</view>
			<view class="u-flex-nowrap u-row-around u-flex page-font">
				<u-form class="u-flex u-flex-nowrap u-row-around">
					<u-form-item>
						<u-input v-model="scanResult" :type="type" height=50 placeholder="请输入条码信息" clearable />
					</u-form-item>
					<u-form-item class="u-flex u-row-center">
						<u-button size="medium" type="success" @click="getPlanNo">{{roleslist[current].name=='试水'?'提交':'查询'}}</u-button>
					</u-form-item>
				</u-form>
				<view class="u-flex-nowrap u-flex-col u-row-center u-col-center" @click="scanFun">
					<u-icon name="scan" color="#2979ff" size="100"></u-icon>
					<view class="$u-type-primary">点击扫码</view>
				</view>
			</view>
		</view>
		
		<view v-show="btnControl" style="margin: 30rpx;" class="u-flex u-row-center u-col-center u-flex-wrap">
			<view style="font-size: 16px; color: red;" v-show="jointSwitch">请先确认<span v-show="ECshow">EC</span><span v-show="FCshow">FC</span>已经质检完毕,再点击检验合格</view>
			<view class="u-flex u-row-center u-col-center buttonGap">
				<u-button size="medium" type="success" @click="showModal">检验合格</u-button>
			</view>
			<view class="u-flex u-row-between u-col-center" style="margin: 30rpx; width: 690rpx;">
				<u-tag v-show="ECshow" text="EC" type="error" />
				<u-tag v-show="FCshow" text="FC" type="error" />
			</view>
		</view>
		<!-- 条形码包含的工位信息 -->
		<view>
			<view class="list-item-wrapper" v-bind:id="index" v-for="(item,index) in List" :key="index">
				<view class="list-item">
					<view>工序名称</view>
					<view>{{item.otherprocess?'其他工序':item.processName}}</view>
				</view>
				<view class="list-item" v-if="!item.otherprocess">
					<view>责任人工号</view>
					<view>{{item.jobNumber}}</view>
				</view>
				<view class="list-item" v-if="!item.otherprocess">
					<view>排产编号</view>
					<view>{{item.planNo}}</view>
				</view>
				<view class="list-item" v-if="!item.otherprocess">
					<view>工段编号</view>
					<view>{{item.sectionNo}}</view>
				</view>
				<view class="list-item" v-if="!item.otherprocess">
					<view>工位序号</view>
					<view>{{item.stationIdentifying}}</view>
				</view>
				<view class="list-item" v-if="!item.otherprocess">
					<view>扫码时间</view>
					<view>{{item.scanTime}}</view>
				</view>
				<view class="u-flex u-row-center">
					<u-button size="medium" :type="item.otherprocess?'primary':'warning'" @click="goFillForm(item)">记录问题</u-button>
					<u-button size="medium" style="margin: 10rpx;" type="success" @click="downMaintenance(item)"  v-if="!item.otherprocess && user.departmentName == 'EMS总装车间'">指定线下维修</u-button>
				</view>
			</view>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-modal v-model="showlinji" :content="contentlinji" :show-cancel-button="cancleBtnlinji" @cancel="cancellinji" @confirm="confirmlinji"></u-modal>
		<u-modal v-model="show" :content="content" :async-close="true" :show-cancel-button="cancleBtn" @cancel="cancel" @confirm="confirm"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {getQcCode, qcPass,getFCView, fcPass, getECView, ecPass,SSscan,getSsjcView,SsjcPass,qcPassEMS,getQcViewUp,getfindprotech} from '../utils/index.js'
	export default {
		data() {
			return {
				scanResult: '',							//条形码信息
				planNo: '',								//发送条形码后获取的planNo
				sectionNo: '',							//工段
				show: false,							//弹出框是否显示的条件
				showlinji: false,
				content: '是否确认所有工序均已合格',		//弹出框的提示信息
				contentlinji: '该工单有临技',		//弹出框的提示信息
				cancleBtn: true,						//弹出框的“取消”按钮显示条件
				cancleBtnlinji: true,
				List: [],								//条形码包含的制作工位信息
				checkCount: '',							//已经通过的质检岗位
				checkStationId: '',						//检验人员所在的工位ID
				btnControl: false,
				ECshow: '',
				FCshow: '',
				jointSwitch: true,
				roleId: uni.getStorageSync('roleId'),	//角色id
				roleslist: JSON.parse(uni.getStorageSync('userBindList')),
				roleslist: [{name:'QC'},{name:'OBA'},{name:'试水检测'},{name:'试水'}],
				current: 0,
				user: uni.getStorageSync('user'),
				AllData: {}
			}
		},
		methods: {
			change(e) {
				this.current = e
				this.btnControl = false
				this.List = []
					// Object.assign(this.$data, this.$options.data.call(this))
			},
			// 点击按钮，启用扫码函数
			scanFun() {
				let that = this
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['barCode','qrCode','datamatrix','pdf417'],
					success: function (res) {
						that.scanResult = res.result
						if(that.scanResult.includes('06S') && that.user.departmentName == 'EMS总装车间'){that.scanResult = that.scanResult.match(/06S(\S*)1P/)[1]}
					},
					fail() {
						console.log('识别失败')
					}
				})
			},
			// 弹出框“确认”绑定的函数
			// 在调用提交函数后关闭弹出框
			confirm() {
				this.allMeetStandard()
				this.show = false
			},
			// 弹出框“取消”绑定的函数
			// 关闭弹出框
			cancel() {
				this.show = false
			},
			// 打开弹出确认框
			showModal() {
				this.show = true
			},
			// 判断是否为QC身份
			qcJudge(){
				let roleId = uni.getStorageSync('roleId')
				if(roleId == 3) {
					if(this.roleslist.length) {
						if(this.roleslist[this.current].name == 'QC'){
							return true
						} else {
							return false
						}
					} else {
						return true
					}
				} else {
					return false
				}
			},
			// 通过条形码获取planNo，随后与条形码内容一起发到后台获取列表信息
			async getPlanNo() {
				let data = ''
				this.List = []
				this.btnControl = false
				this.AllData = {}
				if(!this.scanResult) return this.$refs.uTips.show({
						title: '请输入条码信息',
						type: 'warning',
						duration: '2000'
				})
				// console.log(uni.getStorageSync('roleId'),this.roleslist[this.current].name);
				switch(uni.getStorageSync('roleId')) {
					// 	qc
					case 3: {
						if(this.roleslist.length) {
						if(this.roleslist[this.current].name == '试水检测'){
							data = await getSsjcView({deviceBarcode: this.scanResult,processName: 'SSJC'})
						} else if(this.roleslist[this.current].name == '试水') {
							data = await SSscan({deviceBardcode: this.scanResult,processName: 'SS'})
							return this.$refs.uTips.show({
								title: data.data.msg,
								type: 'success',
								duration: '2000'	
							})
						} else if(this.user.departmentName == 'EMS总装车间' && this.roleslist[this.current].name == 'QC') {
							data = await getQcCode({deviceBarcode: this.scanResult,processName: 'QC'})
						} else if(this.user.departmentName == 'EMS总装车间' && this.roleslist[this.current].name == 'OBA') {
							data = await getQcViewUp({deviceBarcode: this.scanResult,processName: 'OBA'})
						} else {
							data = await getQcCode({deviceBarcode: this.scanResult,processName: 'QC'})
							if(this.user.departmentName != 'EMS总装车间' && data.data.data){data.data.data.checkScaninfoView.unshift({otherprocess: true,planNo: data.data.data.planNo})}
						}
						} else {
							data = await getQcCode({deviceBarcode: this.scanResult,processName: 'QC'})
							if(this.user.departmentName != 'EMS总装车间' && data.data.data){data.data.data.checkScaninfoView.unshift({otherprocess: true,planNo: data.data.data.planNo})}
						}
					}
					break;
					// ec
					case 5: {
						data = await getECView({deviceBarcode: this.scanResult})
					}
					break;
					// fc
					case 6: {
						data = await getFCView({deviceBarcode: this.scanResult})
					}
					break;
					// ss（试水）
					case 10: {
						data = await SSscan({deviceBardcode: this.scanResult,processName: 'SS'})
						if(data.data.code == '1') return this.$refs.uTips.show({
							title: data.data.msg,
							type: 'success',
							duration: '2000'	
						})
						return this.$refs.uTips.show({
							title: data.data.msg,
							type: 'warning',
							duration: '2000'
						})
					}
					break;
					// ssjc（试水检测）
					case 11: {
						data = await getSsjcView({deviceBarcode: this.scanResult,processName: 'SSJC'})
					}
					break;
					// ems
					case 33: {
						data = await getQcCode({deviceBarcode: this.scanResult})
					}
					break;
					// ems
					case 34: {
						data = await getQcCode({deviceBarcode: this.scanResult})
					}
					break;
				}
				const errMsg = data.data.msg
				console.log('errMsg',errMsg);
				if(data.data.code == '1') {
					// 判断是否为QC
					if(this.qcJudge()){
						//ECCheck
						//首产1，去拍照
						//非首产0
						if(data.data.data.ECCheck){
							let ecnImgUrl = uni.getStorageSync('ecnImgUrl')
							if(!ecnImgUrl){
								uni.navigateTo({
									url:'../pages/QC/childPage/photos'
								})
								return console.log('EC首产，拍照上传')
							}
						}
					}
					this.$refs.uTips.show({
						title: '查询成功',
						type: 'success',
						duration: '2000'	
					})
					if(this.user.departmentName == 'EMS总装车间') {
						data.data.data.checkScaninfoView.unshift({otherprocess: true,planNo: data.data.data.planNo}) 
						this.findLinji(data.data.data.OrderType,data.data.data.WorkOrderNo)
					}
					this.List = data.data.data.checkScaninfoView
					this.AllData = data.data.data
					if(this.List && this.List.length) {	
						this.btnControl = true
					}
					this.planNo = data.data.data.planNo
					console.log(data.data.data,'data.data.data')
					this.checkCount = data.data.data.checkCount
					this.sectionNo = data.data.data.sectionNo
					this.checkStationId = data.data.data.checkStationId
					if(this.user.departmentName == 'EMS总装车间') return
					this.ECshow = data.data.data.eccheck == '0' ? false : true
					this.FCshow = data.data.data.notStandardCheck == 'N' ? false : true
					this.jointSwitch = this.ECshow || this.FCshow
				} else {
					this.$refs.uToast.show({
										title: errMsg,
										type: 'warning',
										url: '2000'
									})
				}
			},
			async findLinji(OrderType,WorkOrderNo){
				let query = {
					OrderType,
					WorkOrderNo
				}
				let { data } = await getfindprotech(query)
				if(data.code == '1') {
					if(data.data.processOrder){
						// this.$refs.uTips.show({
						// 	title: '该工单有临技！',
						// 	type: 'warning',
						// 	duration: '2000'
						// })
						this.showlinji = true
					}
				}
			},
			confirmlinji(){
				
			},
			cancellinji(){
				this.showlinji = false
			},
			// 产品所有工序合格函数
			async allMeetStandard() {
				let query = {}
				let ecnImgUrl = uni.getStorageSync('ecnImgUrl')
				if(this.roleslist[this.current].name == '试水检测'){
					query.processName = 'SSJC'
				} else if(this.roleslist[this.current].name == '试水'){
					query.processName = 'SS'
				} else {
				    query.processName = this.roleslist[this.current].name
				}
				query.sectionNo = this.sectionNo
				query.planNo = this.planNo
				query.deviceBarcode = this.scanResult
				query.checkCount = this.checkCount
				query.checkStationId = this.checkStationId
				// 根据不同的角色来选择不同的函数通过质检
				let data = ''
				switch(uni.getStorageSync('roleId')) {
					// 	qc
					case 3: {
						if(ecnImgUrl){
							query.ecnImgUrl = ecnImgUrl	
						}
						if(uni.getStorageSync('departmentName') == 'EMS总装车间' && this.roleslist[this.current].name == 'QC'){
							data = await qcPassEMS(query)
						} else {
							data = await qcPass(query)
						}
					}
					break;
					// ec
					case 5: {
						data = await ecPass(query)
					}
					break;
					// fc
					case 6: {
						data = await fcPass(query)
					}
					break;
					// ssjc(试水检测)
					case 11: {
						data = await SsjcPass(query)
					}
					break;
				}
				const errMsg = data.data.msg
				if(data.data.code == '1') {
					if(ecnImgUrl){
						uni.setStorageSync('ecnImgUrl','')
					}
					this.$refs.uTips.show({
						title: '提交成功',
						type: 'success',
						duration: '2000'
					})
					setTimeout(
						uni.redirectTo({
						    url: '../pages/QC/home/home'
					}), 2000)
				} else {
					this.$refs.uToast.show({
						title: errMsg,
						type: 'error',
						url: '2000'
					})
				}
			},
			//将对应的数据传递到errorList页面
			goFillForm(item) {
				if(this.user.departmentName == 'EMS总装车间'){
					if(item.otherprocess){
						uni.navigateTo({
							url:"../../../pagesA/publicPage/emsQualityForm?orderPlanNo=" + item.planNo
								+ '&submitProcess=' + this.roleslist[this.current].name
								+ '&otherprocess=' + item.otherprocess
						})
					} else {
						uni.navigateTo({
							url:"../../../pagesA/publicPage/emsQualityForm?orderPlanNo=" + item.planNo
								+ '&stationId=' + item.stationId 
								+ '&jobNumber=' + item.jobNumber 
								+ '&deviceBarCode=' + this.AllData.deviceBarcode
								+ '&sectionNo=' + item.sectionNo
								+ '&scanTime=' + item.scanTime
								+ '&upProcess=' + item.stationIdentifying
								+ '&technologyNo=' + this.AllData.produceNo
								+ '&submitProcess=' + this.roleslist[this.current].name
						})
					}
				} else if(!item.otherprocess){
					uni.navigateTo({
						url:"../../../publicPage/qualityForm?planNo=" + item.planNo
							+ '&stationId=' + item.stationId 
							+ '&jobNumber=' + item.jobNumber 
							+ '&deviceBarcode=' + this.scanResult
							+ '&sectionNo=' + item.sectionNo
							+ '&checkStationId=' + this.checkStationId
					})
				} else {
					uni.navigateTo({
						url:"../../../publicPage/qualityForm?otherprocess=" + item.otherprocess
							+ '&deviceBarcode=' + this.scanResult
							+ '&planNo=' + item.planNo
							+ '&checkStationId=' + this.checkStationId
					})
				}
			},
			downMaintenance(item){
				console.log(item)
				uni.navigateTo({
					url:"../../../pages/QC/childPage/downMaintenance?feedbackPlanNo=" + item.planNo
						+ '&deviceBarcode=' + this.AllData.deviceBarcode
						+ '&feedbackWorkOrderNo=' + this.AllData.workOrderNo
						+ '&feedbackOrderType=' + this.AllData.OrderType
						+ '&feedbackProcessOrder=' + item.scanProcessOrder
						+ '&processName=' + item.scanProcessName
						+ '&feedbackName=' + ''
				})
			}
		},
		onShow() {
			console.log('refresh')
		},
		onUnload() {
			uni.setStorageSync('ecnImgUrl','')
		}
	}
</script>

<style lang="scss" scoped>
.page-font {
	font-size: 40rpx;
}
.list-item-wrapper {
	margin: 20rpx;
	padding: 20rpx;
	background-color: #FFEFD5;
	border-radius: 10rpx;
}
.list-item {
	display: flex;
	justify-content: space-between;
	font-size: 16px;
	margin: 10rpx 10rpx;
}
.buttonGap {
	margin-top: 20rpx;
}
</style>
